<?php
/**
 * Registration controller
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Usermanagement Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class usermanagement_RegistrationController extends Bel_Controller {
	
	/**
	 * Session to save form data after registration errors
	 *
	 * @var Zend_Session_Namespace
	 */
	private $_formdata;
	
	/**
	 * Initialization
	 *
	 */
	public function init(){
		parent::init();
		$this->_formdata = new Zend_Session_Namespace ( 'formdata' );
	}
	
	/**
	 * Displays registration form. Form is populates if formdata session 
	 * contains info
	 * 
	 */
	public function indexAction(){
		$form = new Bel_Forms_Builder('registration_form','/usermanagement/registration/register');
		
		if($this->_formdata->values){			
			$form->populateForm($this->_formdata->values);
			$this->_formdata->unsetAll();			
		}
		
		$this->view->assign('messages', $this->_messages->getMessages());
		$this->view->assign('form',$form);
		$this->view->display('forms/default.tpl');
	}
	
	/**
	 * Adds user to DB and sends email with instructions on how to activate account.
	 * User is added after validations (username, email, password match) 
	 *
	 */
	public function registerAction(){		 
		$dataToAdd = $this->_request->getParams();
		
		$oValidate = new Helpers_Usermanagement_Validate();
		$error = false;
		
		if(!$oValidate->ifUsernameAvailable($dataToAdd['user_login'])){
			$this->_messages->setMessage('User with such username already exists','error','user_login');
			$error = true;
		}

		if(!$oValidate->ifEmailAvailable($dataToAdd['user_email'])){
			$this->_messages->setMessage('User with such email already exists','error','user_email');
			$error = true;
		}		
		
		if(!$oValidate->ifPasswordMatch($dataToAdd['user_password'],$dataToAdd['password2'])){
			$this->_messages->setMessage('Passwords entered do not match','error','user_password');
			$error = true;
		}
		
		if(!$oValidate->validateCaptcha($dataToAdd)){
			$this->_messages->setMessage('Wrong captcha field','error','captcha');
			$error = true;
		}	
		
				
		if($this->_validator->validate('registration_form',$dataToAdd) && !$error){			
			
			$oGroups = new Models_Usermanagement_Groups();
			$dataToAdd['user_group_id'] = $oGroups->getDefaultGroup();
			$dataToAdd['user_password'] = md5($dataToAdd['user_password']); 
			$dataToAdd['user_confirmation_code'] = md5(time()+rand(10000,99999));
			$dataToAdd['user_active'] = 0;
			
			
			$oUsers = new Models_Usermanagement_Users();
			
			if($oUsers->addEntry($dataToAdd)){
				$this->_sendConfirmationEmail($dataToAdd['user_email'],$dataToAdd['user_login'],$dataToAdd['user_confirmation_code']);
				$this->_messages->setMessage('Thank you for your registration! Please check your email to verify your email address');
				$this->_redirect('/');	
			}
			
		}else{
			foreach ( $this->_validator->getErrors () as $field=>$error ) {
				$this->_messages->setMessage ( $error, 'error' , $field);
			}
			
			unset($dataToAdd['captcha']);					
			$this->_formdata->values = $dataToAdd;			
			$this->_redirect('/register/');
		}
	}
	
	/**
	 * Activates user account after clicking link from email
	 *
	 */
	public function confirmAction(){
		$oUsers = new Models_Usermanagement_Users();		
		if($oUsers->activateUser($this->_request->getParam('code'))){
			$this->_messages->setMessage('Your account has been activated');
		}else{
			$this->_messages->setMessage('Wrong activation code or account is already active','error');
		}
		$this->_redirect('/');
	}
	
	/**
	 * Send user email with instructions on how to activate an account
	 *
	 * @param string $email_to
	 * @param string $name_to
	 * @param string $confirmation_code
	 */
	private function _sendConfirmationEmail($email_to, $name_to, $confirmation_code){
		$mailer = new Bel_Mailer();
		$mailer->addTo ( $email_to );
		$mailer->setSubject('Confirm your registration');
		$this->view->assign('code',$confirmation_code);
		$this->view->assign('name',$name_to);
		$mailer->setBodyHtml ( $this->view->fetch('usermanagement/registration/confirmation_email.tpl') );
		$mailer->send ();
	}
	
}